/**
 * @file bsp_ethernet.h
 * @author zdk
 * @brief 以太网接口抽象层
 * @version 0.1
 * @date 2021-07-08
 * 
 * BSD 3-Clause License
 * 
 * Copyright (c) 2021, water_zhang
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

#if !defined(BSP_ETHERNET_H)
#define BSP_ETHERNET_H

#include <stdint.h>

/**
 * @brief 数据包接收回调函数
 * 
 * @param param 用户自定义参数
 * 
 */
typedef void (*ethernet_cb)(void *param);

typedef enum
{
    ETH_OK,
    ETH_ERR,
    ETH_BUSY,
    ETH_NODATA,
} eth_result;

typedef struct
{
    struct 
    {
        uint8_t EIE;
        uint8_t EIR;
        uint8_t ESTAT;
        uint8_t ECON2;
        uint8_t ECON1;
    } nobank;
    struct 
    {
        uint8_t ERXFCON;
        uint8_t EPKTCNT;
    } bank1;
    struct
    {
        uint8_t MACON1;
        uint8_t MACON2;
        uint8_t MACON3;
        uint8_t MACON4;
    } bank2;
    uint8_t irq_level;
} eth_info, *eth_info_t;


/* 中断标志位 */
#define ETH_FLAG_PKT        (0x40) /* 接收数据包待处理中断标志位 */
#define ETH_FLAG_DMA        (0x20) /* DMA 中断标志位 */
#define ETH_FLAG_LINK       (0x10) /* 连接状态改变中断标志位 */
#define ETH_FLAG_TX         (0x08) /* 发送中断标志位 */
#define ETH_FLAG_WOL        (0x04) /* WOL 中断标志位 */
#define ETH_FLAG_TXER       (0x02) /* 发送错误中断标志位 */
#define ETH_FLAG_RXER       (0x01) /* 接收错误中断标志位 */

eth_result Bsp_Ethernet_Init(void);
eth_result Bsp_Ethernet_GetAddr(uint8_t *addr, uint8_t len);
eth_result Bsp_Ethernet_Transmit(uint8_t *pBuff, uint16_t len);
eth_result Bsp_Ethernet_Receive(uint8_t *pBuff, uint16_t len, uint16_t *byte_read);

eth_result Bsp_Ethernet_GetInfo(eth_info *info);

void Bsp_Ethernet_RegisterRecvCallback(ethernet_cb cb, void *param);
void Bsp_Ethernet_EnableINT(uint8_t flag);
void Bsp_Ethernet_DisableINT(uint8_t flag);
uint8_t Bsp_Ethernet_GetInterruptFlag(void);

#endif // BSP_ETHERNET_H
